package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.SysUserRoleService;
import com.atguigu.spzx.model.entity.sys.SysRole;
import com.atguigu.spzx.model.entity.sys.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2023/12/30
 * description：
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysRole> getAssignRoles(Long id) {
        return sysUserRoleMapper.getAssignRoles(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAssignRoles(Long userId, List<Long> roleIds) {
        //删除之前的
         sysUserRoleMapper.deleteById(userId);

        //插入更新之后的
        if(!CollectionUtils.isEmpty(roleIds)){
            sysUserRoleMapper.insertAssign(userId,roleIds);
        }
    }
}
